package com.zhl.parameter;

public class MethodTest3 {
    public static void main(String[] args) {
        //目标：打印一个int数组的内容
        int[] arr = {1,2,3,4,5};

        PrintInArray(arr);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        PrintInArray2(arr);

        int[] arr2 = null;
        PrintInArray3(arr2);
    }




        //方法一：
    public static void PrintInArray(int[] arr)
    {
        System.out.print("[");
        for(int i=0;i<arr.length;i++)
        {
            if(i != arr.length-1)
                {
                    System.out.print(arr[i]+",");
                }
            if(i == arr.length-1)
                {
                    System.out.print(arr[i]+"]");
                }
        }
    }

    //方法二：
    public static void PrintInArray2(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++){
            System.out.print(i == arr.length - 1? arr[i] + "]" : arr[i] + ",");
        }
    }

    //为了防止传的数组是null，可以加一个判断：
    public static void PrintInArray3(int[] arr) {
        if (arr == null) {
            System.out.println("数组为空");
            return;
        }
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] + "]" : arr[i] + ",");
        }
    }


}
